package com.infindo.frame.util;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
//import android.util.Log;

import com.tiantian.app.data.mo.XmlMessage;

public class PictureUtil {
	public static String getMimeType(String fileName) {
		if (fileName == null)
			return null;
		
		int dotLast = fileName.lastIndexOf(".");
		if (dotLast < 1)
			return null;
		
		String type = fileName.substring(dotLast + 1);
		
		String mimeType = null;
		if (type.equalsIgnoreCase("PNG")) {
			mimeType = "image/png";
		} else if (type.equalsIgnoreCase("BMP")) {
			mimeType = "image/bmp";
		} else if (type.equalsIgnoreCase("JPEG") || type.equalsIgnoreCase("JPE") || type.equalsIgnoreCase("JPG")) {
			mimeType = "image/jpeg";
		} else if (type.equalsIgnoreCase("GIF")) {
			mimeType = "image/gif";
		} else if (type.equalsIgnoreCase("TIFF") || type.equalsIgnoreCase("TIF")) {
			mimeType = "image/tiff";
		}
		
		return mimeType;
	}
	
	public static String getImgUrl(String imgurl) {
		// TODO Auto-generated method stub
	    int start = imgurl.indexOf("<img class=\"aligncenter\" src=\"");
        if(start!=-1){
        	imgurl = imgurl.substring(start+30);
        	int end = imgurl.indexOf("\"");
        	if(end!=-1){
        		
        		imgurl = imgurl.substring(0,end);
//        		Log.e("getPhotoUrl","*****************   "+imgurl);
        		return imgurl;
        	}
        }
	    return null;
	}
	
	
	public static String getPhotoUrl(String imgurl) {
		// TODO Auto-generated method stub
	
		int start = imgurl.indexOf("<img src=\"");
		if(start!=-1){
			imgurl = imgurl.substring(start+10);
			int end = imgurl.indexOf("\"");
			if(end!=-1){
				imgurl = imgurl.substring(0,end);
//				Log.e("getPhotoUrl","*****************  2222     "+imgurl);
				return imgurl;
			}
		}
		return null;
	}
	
	
	public static String getComingEventUrl(String url) {
		// TODO Auto-generated method stub
		
		int start = url.indexOf("<a href=\"");
		if(start!=-1){
			url = url.substring(start+9);
			int end = url.indexOf("\"");
			if(end!=-1){
				url = url.substring(0,end);
//				Log.e("getPhotoUrl","*****************  2222     "+imgurl);
				return url;
			}
		}
		return null;
	}
	
	public static Bitmap getBitMap(URL imgUrl) {
		// TODO Auto-generated method stub
		Bitmap bm = null;
		try{
			HttpURLConnection hc = (HttpURLConnection) imgUrl.openConnection();
			hc.connect();
			bm = BitmapFactory.decodeStream(hc.getInputStream());
			hc.getInputStream().close();
			hc.disconnect();
		}catch(Exception e){}
		return bm;
	}
	
	public static List<Drawable[]> getDrawable(List<XmlMessage> list){
		List<Drawable[]> li = new ArrayList<Drawable[]>();
		Drawable[] dr = new Drawable[list.size()];
		Drawable[] dra = new Drawable[4];
		
		Iterator<XmlMessage> iter = list.iterator();
		int i = 0;
		int n = 0;
		while(iter.hasNext()){
			XmlMessage xm = iter.next();
			Drawable draw = xm.getImage();
			dr[i] = draw;
			i++;
		}
		for(int k =0;k <dr.length;k++){			
			dra[n] = dr[k];
		    n++;
		    if(n>3){
		    	li.add(dra);
//		    	System.out.println(li);
		    	n=0;
		    }
		}
		return li;
		
	}
}
